﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace ImbedLibrary
{
    public static class MyString
    {

        /// <summary>
        /// 格式化url
        /// 例如 http://google.com/ 返回 google.com
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FormatUrl(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            string[] oldChar ={"http://","https://"};
            for (int i = 0; i < oldChar.Length; i++)
			{
                str = str.Replace(oldChar[i],"");
			}
            if (str.IndexOf('/') != -1)
            {
                return str.Remove(str.IndexOf('/'));
            }
            return str;
        }

        /// <summary>
        /// 匹配 html <P> 标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static MatchCollection TailorPLabel(string str)
        {
            Regex r = new Regex("<p>.*?</p>",RegexOptions.IgnoreCase);
            return r.Matches(str);
        }

        /// <summary>
        /// 返回Access连接字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String GetAccessPath(this String str)
        {
            return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str + ";Persist Security Info=True";
        }

        /// <summary>
        /// 返回文件夹名字
        /// 例: c:\temp 返回 temp
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FolderName1(this string str)
        {
            str = str.Path2();
            string[] split = str.Split('\\');
            if (split.Length == 0)
            {
                return str;
            }
            int count = split.Length;
            return split[count -1];
        }

        /// <summary>
        /// 从路径中获取文件夹名字
        /// 例: c:\temp\hello.txt 返回 temp
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FolderName(this string str)
        {
            string[] split = str.Split('\\');
            if (split.Length == 0) return str;
            int count = split.Length;
            if (count >= 2) return split[count - 2];
            return split[count - 1];
        }

        /// <summary>
        /// 从文件路径中获取文件夹 
        /// 例: c:\temp\hello.txt 返回 c:\temp\
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Folder(this string str)
        {
            return str.Remove(str.LastIndexOf("\\") + 1);
        }

        /// <summary>
        /// 从文件路径中获取文件夹
        /// 例: c:\temp\hello.txt 返回 c:\temp
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Folder2(this string str)
        {
            return str.Remove(str.LastIndexOf("\\"));
        }

        /// <summary>
        /// 获取文件后缀
        /// </summary>
        /// <param name="str">文件路径</param>
        /// <returns></returns>
        public static string GetEx(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            int count = str.LastIndexOf(".");
            if (count != -1)
                return str.Substring(count + 1);
            return str;
        }

        /// <summary>
        /// 从路径中获取文件名字
        /// 例: 路径 c:\temp\hello.txt 返回 hello.txt
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetFileName(this string str)
        {
            int count = str.LastIndexOf("\\");
            if (count != -1)
            {
                return str.Substring(count + 1);
            }
            return str;
        }

        /// <summary>
        /// 从文件名中获取不带后缀的文件名
        /// 例: systemlog.txt 返回 systemlog
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetFileName1(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            int count = str.LastIndexOf(".");
            if (count == -1)
            {
                return str;
            }
            return str.Remove(count);
        }

        /// <summary>
        /// 修复文件夹路径
        /// 例: 路径 c:\temp 返回 c:\temp\
        /// </summary>
        /// <param name="path">路径 c:\temp</param>
        /// <returns>c:\temp\</returns>
        public static string Path(this string path)
        {
            if (path.LastIndexOf("\\") + 1 == path.Length) return path;
            else return path + "\\";
        }

        /// <summary>
        /// 修复文件夹路径 
        /// 例: 路径 c:\temp\ 返回 c:\temp
        /// </summary>
        /// <param name="str">路径 c:\temp\</param>
        /// <returns>c:\temp</returns>
        public static string Path2(this string str)
        {
            if (str == "\\")
            {
                return str;
            }
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (str.LastIndexOf("\\") + 1 == str.Length)
            {
                return str.Remove(str.LastIndexOf("\\"));
            }
            
            return str;
        }

        /// <summary>
        /// 去除文本末尾的换行符
        /// </summary>
        /// <param name="str">文本</param>
        /// <returns></returns>
        public static string TrimRN(this String str)
        {
            if (str != "" && str != null)
            {
                int length = str.Length;
                int begin = str.LastIndexOf("\r\n");
                if (begin + 2 == length)
                {
                    return str.Substring(0, length - 2);
                }
            }
            return str;
        }

        /// <summary>
        /// 是否是IP
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIP(this string str)
        {
            return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 转换成int32
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Int32 ToInt32(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 去除字符串首尾的 斜杠
        /// 例如: \Lib\  返回 Lib
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Sprit(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (str == "\\")
            {
                return str;
            }
            int startIndex = str.IndexOf("\\");
            int endIndex = -1;
            if (startIndex == 0)
            {
                str = str.Substring(1, str.Length - 1);
            }
            endIndex = str.LastIndexOf("\\") + 1;
            if (endIndex == str.Length)
            {
                str = str.Remove(endIndex - 1);
            }
            return str;
        }
    }
}
